import 'package:gameplay_flutter/lib_runtime/extension/string_extension/string_validate.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';
import 'package:crypto/crypto.dart';
import 'dart:convert';

class PersistentUUID {
  static const _storageKey = 'cr_uuid_key';

  /// 获取或生成持久化 UUID
  static Future<String> getCRUUIDString() async {
    final prefs = await SharedPreferences.getInstance();
    String? storedUUID = prefs.getString(_storageKey);

    // 不存在则生成新 UUID
    if (storedUUID == null) {
      final newUUID = _generateUUID();
      await prefs.setString(_storageKey, newUUID);
      return newUUID;
    }

    return storedUUID;
  }

  /// 生成带 MD5 的 UUID
  static String _generateUUID() {
    final rawUUID = const Uuid().v4(); // 需要添加 uuid 包依赖
    return rawUUID.toMd5();
  }
}
